package com.example.chatgpt.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.chatgpt.R;
import com.example.chatgpt.bean.Message;
import com.example.chatgpt.entity.MessageInfo;
import com.example.chatgpt.util.TimeUtil;

import java.util.List;

public class MessageInfoAdapter extends RecyclerView.Adapter<MessageInfoAdapter.MyInfoViewHolder> {

    List<MessageInfo> messageList;

    public MessageInfoAdapter(List<MessageInfo> messageList) {
        this.messageList = messageList;
    }

    public void setList(List<MessageInfo> list){
        this.messageList = list;
    }

    @NonNull
    @Override
    public MyInfoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, null);
        MyInfoViewHolder holder = new MyInfoViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyInfoViewHolder holder, int position) {
        Log.d("guaimiao" , messageList.get(position).getMessage());
        if (messageList.get(position).getSetBy().equals(Message.SENT_BY_ROBOT)) {
            holder.rl_me.setVisibility(View.GONE);
            holder.rl_robot.setVisibility(View.VISIBLE);
            holder.tv_robot.setText(messageList.get(position).message);
            holder.tv_date.setVisibility(View.GONE);
        } else {
            holder.rl_robot.setVisibility(View.GONE);
            holder.rl_me.setVisibility(View.VISIBLE);
            holder.tv_me.setText(messageList.get(position).message);
            holder.tv_date.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }


    public class MyInfoViewHolder extends RecyclerView.ViewHolder{

        RelativeLayout rl_robot;
        RelativeLayout rl_me;
        TextView tv_robot;
        TextView tv_me;
        TextView tv_date;
        public MyInfoViewHolder(@NonNull View itemView) {
            super(itemView);
            rl_robot = itemView.findViewById(R.id.rl_robot);
            rl_me = itemView.findViewById(R.id.rl_me);
            tv_robot = itemView.findViewById(R.id.tv_robot);
            tv_me = itemView.findViewById(R.id.tv_me);
            tv_date = itemView.findViewById(R.id.tv_date);
        }
    }


}
